﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Open.Help;
using System.Net;
using System.Text.RegularExpressions;

namespace Open
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lvlanguage.CheckBoxes = true;
            listView1.CheckBoxes = true;

            lvlanguage.View = View.List;
            listView1.View = View.List;

            Languages ll = new Languages();

            List<string> langNames = new List<string>(ll.GetPrimaryLanguage());
            List<string> otherMarkets = new List<string>(ll.GetOtherMarkets());

            foreach (string langName in langNames)
            {
                lvlanguage.Items.Add(langName);
            }
            foreach (string marketName in otherMarkets)
            {
                listView1.Items.Add(marketName);
            }
            
        }
        private void OpenIE(string URL)
        {
            System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();
            
            si.Verb = "open";
            si.FileName = URL;
            //si.Arguments = URL;
            si.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
            si.UseShellExecute = true;
            System.Diagnostics.Process.Start(si);
        }
        private void OpenIE(string URL, bool UseShellExecute)
        {
            System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();

            si.Verb = "open";
            si.FileName = "iexplore.exe";
            si.Arguments = URL;
            si.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
            //si.UseShellExecute = true;
            System.Diagnostics.Process.Start(si);
        }
        
        private void button5_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Length < 10)
            {
                MessageBox.Show("Please input right URL");
                return;
            }
            string preURL = "http://";
            string url = this.textBox1.Text;
            if (url.Contains(@"http://"))
            {
                url = url.Substring(7);
            }
            string[] subURL = null;
            subURL = url.Split('/');

            Regex r = new Regex("-");
            int languageMark = 0;

            for (int i = 0; i < subURL.Length; i++)
            {
         
                //For Glink, which language mark contain '-', but length more than 5
                if (this.checkBox1.Checked == true)
                {
                    if (r.Matches(subURL[i]).Count == 1)
                    {
                        languageMark = i;
                    }
                }
                else if (this.chkBOXGlink.Checked == false)
                {
                    //Fiter invalid url,whcih also contains '-'
                    if ((r.Matches(subURL[i]).Count == 1 & subURL[i].Length <= 5) || subURL[i] == "sr-cyrl-cs" || subURL[i] == "sr-latn-cs")
                    {
                        //Find out which sub URL contain language mark, such as ll-cc
                        languageMark = i;
                    }
                }

            }
            OpenIE(preURL + url, false);
            System.Threading.Thread.Sleep(1000);

            //The primary markets
            for (int i = 0; i < this.lvlanguage.CheckedItems.Count; i++)
            {
                //If it's Glink
                if (this.checkBox1.Checked == true)
                {
                    int temp = subURL[languageMark].Length - 5;
                    subURL[languageMark] = subURL[languageMark].Substring(0, temp);
                    subURL[languageMark] = subURL[languageMark] + this.lvlanguage.CheckedItems[i].Text;
                }
                else if (this.chkBOXGlink.Checked == false)
                {
                    if (0 == languageMark & url.ToLower().Contains("langid"))
                    {
                        if (i >0)
                            url = url.Replace(this.lvlanguage.CheckedItems[i-1].Text, this.lvlanguage.CheckedItems[i].Text);
                        else 
                            url = url.Replace("en-us",this.lvlanguage.CheckedItems[i].Text);
                    }
                    else
                    {
                        subURL[languageMark] = this.lvlanguage.CheckedItems[i].Text;
                    }
                    
                }

                //Add logic for BOX GLink
                if (true == this.chkBOXGlink.Checked)
                {
                    subURL[1] = subURL[1].Substring(0, 5) + this.lvlanguage.CheckedItems[i].Text;
                }

                if (!(0 == languageMark & url.ToLower().Contains("langid")))
                    url = string.Join("/",subURL);
                System.Threading.Thread.Sleep(1000);
                OpenIE(preURL + url);
            }
            System.Threading.Thread.Sleep(1000);

            //The secondary markets
            if (this.listView1.CheckedItems.Count > 0)
            {
                OpenIE(preURL + url, false);
                for (int i = 0; i < this.listView1.CheckedItems.Count; i++)
                {
                    if (this.checkBox1.Checked == true)
                    {
                        int temp2 = subURL[languageMark].Length - 5;
                        subURL[languageMark] = subURL[languageMark].Substring(0, temp2);
                        subURL[languageMark] = subURL[languageMark] + this.listView1.CheckedItems[i].Text;
                    }
                    else
                    {
                        if (0 == languageMark & url.ToLower().Contains("langid"))
                        {
                            if (i>0)
                                url = url.Replace(this.listView1.CheckedItems[i-1].Text, this.listView1.CheckedItems[i].Text);
                            else 
                                url = url.Replace("en-us", this.listView1.CheckedItems[i].Text);
                        }
                        else
                        {
                            subURL[languageMark] = this.listView1.CheckedItems[i].Text;
                        }
                    }
                    if (!(0 == languageMark & url.ToLower().Contains("langid")))
                        url = string.Join("/", subURL);
                    System.Threading.Thread.Sleep(1000);
                    OpenIE(preURL + url);
                }
            }

        }

        private void btnPrimary_Click(object sender, EventArgs e)
        {
             for (int i = 0; i < this.lvlanguage.Items.Count; i++)
             {
                 this.lvlanguage.Items[i].Checked = true;
             }
        }

        private void btnOtherLanguage_Click(object sender, EventArgs e)
        {
            for (int j = 0; j < this.listView1.Items.Count; j++)
            {
                this.listView1.Items[j].Checked = true;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.lvlanguage.Items.Count; i++)
            {
                this.lvlanguage.Items[i].Checked = false;
            }
            for (int j = 0; j < this.listView1.Items.Count; j++)
            {
                this.listView1.Items[j].Checked = false;
            }
        }

        private void toolTip1_Popup(object sender, PopupEventArgs e)
        {

        }

        private void chkBOXGlink_CheckedChanged(object sender, EventArgs e)
        {
            Languages ll = new Languages();
            if (chkBOXGlink.Checked == true)
            {
                this.lvlanguage.Clear();
                List<string> BOXGlinklanguages = new List<string>(ll.GetBOXGlinkLanguage());

                foreach (string langName in BOXGlinklanguages)
                {
                    lvlanguage.Items.Add(langName);
                }
            }
            else
            {
                this.lvlanguage.Clear();
                List<string> langNames = new List<string>(ll.GetPrimaryLanguage());
                foreach (string langName in langNames)
                {
                    lvlanguage.Items.Add(langName);
                }
            }
        }


    }
}
